********************************************************************************
* ELECTORAL REPERCUSSIONS OF A PANDEMIC:  EVIDENCE FROM THE 2009 H1N1 OUTBREAK *
********************************************************************************

// NOVEMBER 2, 2021

// THIS FILE REPLICATES TABLES 1 AND 2 IN THE MAIN TEXT OF "ELECTORAL 
// REPERCUSSIONS OF A PANDEMIC: EVIDENCE FROM THE 2009 H1N1 OUTBREAK" BY
// EMILIO GUTIÉRREZ, JAAKKO MERILÄINEN, AND ADRIÁN RUBLI

global data "XXX" // Insert data directory here
global output "XXX" // Insert output directory here

* Install required packages

ssc install reghdfe, replace
ssc install estout, replace

* Open data

use "$data/GMR_data_main.dta", clear

********************************************************************************

* Table 1
* H1N1 and electoral performance of the incumbent: difference-in-differences estimates

egen munid=group(estado municipio)

gen year2=year*year // Generate year^2

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

est clear

eststo: reghdfe PAN_voteshare 1.post##c.change_ari [aw = total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare 1.post##c.change_ari_z [aw = total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PAN_voteshare 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PAN_voteshare 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PAN_voteshare 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore


preserve

gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PAN_voteshare 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PAN_voteshare 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

preserve

gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PAN_voteshare 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore


esttab est*  using "$output/table1.tex", replace noomitted nobaselevels booktabs  mlabels(none) ///
	compress lines star(* .1 ** .05 *** .01) label ///
	mgroups("2006-2009" "1997-2012", pattern(1 0 1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	b(%9.3f) se(%9.3f) stats(N1 N2 N3 district stateyear linear quadratic, labels("Number of observations" "Number of sections" "Number of clusters" "Section FE" "Municipality-year FE" "Linear time trend" "Quadratic time trend") ///
	fmt(0 0 0)) keep(1.post#c.change_ari 1.post#c.change_ari_z) ///
	coeflabels(1.post#c.change_ari "1[Year $\geq$ 2009] $\times$ Excess ARI cases (1000s)" ///
	1.post#c.change_ari_z "1[Year $\geq$ 2009] $\times$ Excess ARI cases (z-score)") ///
	order(1.post#c.change_ari 1.post#c.change_ari_z) ///
	nodepvars nomtitle prehead(\begin{tabular}{l*{9}{c}} \toprule) ///
	postfoot(\bottomrule \end{tabular})
	
********************************************************************************

* Table 2
* Effect heterogeneity by the timing and the height of the peak

est clear

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari##i.timing [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari = 2009.year#2.timing#c.change_ari
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari_z##i.timing [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari_z = 2009.year#2.timing#c.change_ari_z
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari##i.height [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari = 2009.year#2.height#c.change_ari
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari_z##i.height [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari_z = 2009.year#2.height#c.change_ari_z
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari##i.distance2 [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari = 2009.year#2.distance2#c.change_ari
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari_z##i.distance2 [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari_z = 2009.year#2.distance2#c.change_ari_z
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore


preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari##i.coverage [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari = 2009.year#2.coverage#c.change_ari
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari_z##i.coverage [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari_z = 2009.year#2.coverage#c.change_ari_z
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari##i.pan [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari = 2009.year#1.pan#c.change_ari
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PAN_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PAN_voteshare ib2006.year##c.change_ari_z##i.pan [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
test 2009.year#c.change_ari_z = 2009.year#1.pan#c.change_ari_z
estadd scalar f = r(F)
estadd scalar p = r(p)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

restore

esttab est*  using "$output/table2.tex", replace noomitted nobaselevels booktabs  mlabels(none) ///
	compress lines star(* .1 ** .05 *** .01) label ///
	mgroups("Timing of peak" "Height of peak" "Distance from healthcare" "Insurance coverage" "PAN governor", pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	b(%9.3f) se(%9.3f) stats(N1 N2 N3 f p district stateyear, labels("Number of observations" "Number of sections" "Number of clusters" "\textit{F}-statistic" "\textit{p}-value" "Section FE" "Municipality-year FE") ///
	fmt(0 0 0 2)) keep(2009.year#c.change_ari 2009.year#c.change_ari_z ///
	2009.year#2.timing#c.change_ari 2009.year#2.timing#c.change_ari_z ///
	2009.year#2.height#c.change_ari 2009.year#2.height#c.change_ari_z ///
	2009.year#2.coverage#c.change_ari 2009.year#2.coverage#c.change_ari_z ///
	2009.year#2.distance2#c.change_ari 2009.year#2.distance2#c.change_ari_z ///
	2009.year#1.pan#c.change_ari 2009.year#1.pan#c.change_ari_z) ///
	coeflabels(2009.year#c.change_ari "1[Year = 2009] $\times$ Excess ARI cases (1000s)" ///
	2009.year#c.change_ari_z "1[Year = 2009] $\times$ Excess ARI cases (z-score)" ///
	2009.year#2.timing#c.change_ari "1[Year = 2009] $\times$ Excess ARI cases (1000s) $\times$ Late peak" ///
	2009.year#2.timing#c.change_ari_z "1[Year = 2009] $\times$ Excess ARI cases (z-score) $\times$ Late peak" ///
	2009.year#2.height#c.change_ari "1[Year = 2009] $\times$ Excess ARI cases (1000s) $\times$ High peak" ///
	2009.year#2.height#c.change_ari_z "1[Year = 2009] $\times$ Excess ARI cases (z-score) $\times$ High peak" ///
	2009.year#2.distance2#c.change_ari "1[Year = 2009] $\times$ Excess ARI cases (1000s) $\times$ Far from healthcare" ///
	2009.year#2.distance2#c.change_ari_z "1[Year = 2009] $\times$ Excess ARI cases (z-score) $\times$ Far from healthcare" ///
	2009.year#2.coverage#c.change_ari "1[Year = 2009] $\times$ Excess ARI cases (1000s) $\times$ High insurance coverage" ///
	2009.year#2.coverage#c.change_ari_z "1[Year = 2009] $\times$ Excess ARI cases (z-score) $\times$ High insurance coverage" ///
	2009.year#1.pan#c.change_ari "1[Year = 2009] $\times$ Excess ARI cases (1000s) $\times$ State controlled by PAN" ///
	2009.year#1.pan#c.change_ari_z "1[Year = 2009] $\times$ Excess ARI cases (z-score) $\times$ State controlled by PAN") ///
	order(2009.year#c.change_ari 2009.year#c.change_ari_z ///
	2009.year#2.timing#c.change_ari 2009.year#2.timing#c.change_ari_z ///
	2009.year#2.height#c.change_ari 2009.year#2.height#c.change_ari_z ///
	2009.year#2.distance2#c.change_ari 2009.year#2.distance2#c.change_ari_z ///
	2009.year#2.coverage#c.change_ari 2009.year#2.coverage#c.change_ari_z ///
	2009.year#1.pan#c.change_ari 2009.year#1.pan#c.change_ari_z) ///
	nodepvars nomtitle prehead(\begin{tabular}{l*{11}{c}} \toprule) ///
	postfoot(\bottomrule \end{tabular})
